package com.biodiv.transport.factory;


import com.biodiv.miracle.ioc.context.ApplicationContext;
import com.biodiv.miracle.ioc.factory.FactoryBean;
import com.biodiv.miracle.ioc.factory.FactoryBeanRegister;
import com.biodiv.transport.support.TransportClient;
import com.biodiv.transport.support.TransportOpenClient;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author: tianquan
 * @date: 2025-05-30  09:05
 * @description: 扫描注册Mapper接口
 */
public class TransportRegisterFactory extends FactoryBeanRegister {

    @Override
    public Collection<FactoryBean<?>> scanFactoryBean(Class<?> mianClass, ApplicationContext applicationContext) {
        TransportOpenClient transportOpenClient = mianClass.getAnnotation(TransportOpenClient.class);
        if (transportOpenClient == null) {
            return Collections.emptyList();
        }
        // 扫描所有类，找到有@TransportClient的类
        List<Class<?>> classes = loadClass(mianClass.getPackage().getName());

        List<Class<?>> transportClientClassList = classes.stream().filter(className -> className.isAnnotationPresent(TransportClient.class)).toList();
        System.out.println("Client：" + transportClientClassList);

        // 生成FactoryBean
        List<FactoryBean<?>> factoryBeans = new ArrayList<>();

        for (Class<?> aClass : transportClientClassList) {
            TransportFactoryBean<?> transportFactoryBean = new TransportFactoryBean<>(aClass, applicationContext);
            factoryBeans.add(transportFactoryBean);
        }

        return factoryBeans;
    }

}
